#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
认证数据传输对象

定义认证领域的请求和响应数据结构
"""

from typing import Dict, Any, List, Optional
from pydantic import BaseModel, Field, EmailStr, validator


class RegisterRequest(BaseModel):
    """注册请求DTO"""
    username: str = Field(..., min_length=3, max_length=50)
    password: str = Field(..., min_length=6, max_length=50)
    email: Optional[EmailStr] = None
    name: Optional[str] = Field(None, max_length=50)
    
    @validator('username')
    def username_valid(cls, v):
        if not v.isalnum():
            raise ValueError('用户名只能包含字母和数字')
        return v


class LoginRequest(BaseModel):
    """登录请求DTO"""
    username: str = Field(..., min_length=3, max_length=50)
    password: str = Field(..., min_length=6, max_length=50)


class TokenResponse(BaseModel):
    """令牌响应DTO"""
    token: str
    user_id: str
    username: str


class UserInfoResponse(BaseModel):
    """用户信息响应DTO"""
    user_id: str
    username: str
    email: Optional[str] = None
    name: Optional[str] = None
    token: str
    created_time: str 